package com.zyy.smartcity1129.work;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.JobPostInfo;
import com.zyy.smartcity1129.data.JobProfessionInfo;
import com.zyy.smartcity1129.data.UserInfo;
import com.zyy.smartcity1129.data.homeLbInfo;
import com.zyy.smartcity1129.movie.MovieFragment;
import com.zyy.smartcity1129.util.BannerAd;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.util.RvAd;
import com.zyy.smartcity1129.util.XCUtli;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: LookJobFragment$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 6/12/2021$
 * @Version: 1.0
 */
public class LookJobFragment extends BaseFragment {
    private Banner banner;
    private SearchView search;
    private RecyclerView rv1;
    private RecyclerView rv2;
    List<homeLbInfo.RowsBean> blist = new ArrayList<>();
    List<JobProfessionInfo.RowsBean> jobPList = new ArrayList<>();
    List<JobPostInfo.RowsBean> postList = new ArrayList<>();
    List<JobPostInfo.RowsBean> postsList = new ArrayList<>();
    private int typeId;
    RvAd<JobPostInfo.RowsBean> rvAd2;
    public static JobPostInfo.RowsBean jobPost;
    UserInfo.UserBean userBean;
    int userId;

    @Override
    protected int layId() {
        return R.layout.f_lookjob;
    }

    @Override
    protected String title() {
        return null;
    }

    @Override
    protected boolean back() {
        return false;
    }

    @Override
    protected Boolean bottom() {
        return false;
    }

    @Override
    protected void init(View iv) {
        WorkFragment.mutableLiveData.postValue("找工作");
        initView(iv);
        XCUtli xcUtli = new XCUtli(3);
        xcUtli.then(() -> {
            blist = Http.getInstance().get("/prod-api/api/rotation/list", null, homeLbInfo.class, 1).getRows();
            xcUtli.addint();
        });
        xcUtli.then(() -> {
            jobPList = Http.getInstance().get("/prod-api/api/job/profession/list", null, JobProfessionInfo.class, 1).getRows();
            xcUtli.addint();
        });
        xcUtli.then(() -> {
            postList = Http.getInstance().get("/prod-api/api/job/post/list", null, JobPostInfo.class, 1).getRows();
            xcUtli.addint();
        });
        xcUtli.XCliveData.observe(getViewLifecycleOwner(), a -> {
            if (a.get() == 3) {
                List<Object> b = new ArrayList<>();
                for (homeLbInfo.RowsBean all : blist) {
                    b.add(all.getAdvImg());
                }
                banner.setAdapter(new BannerAd(b)).setBannerRound(16).addBannerLifecycleObserver(this).setLoopTime(3000).setIndicator(new CircleIndicator(getContext())).start();
                List<JobProfessionInfo.RowsBean> jobProList = new ArrayList<>();
                jobProList = jobPList.subList(0, 9);
                RvAd<JobProfessionInfo.RowsBean> rvAd1 = new RvAd<JobProfessionInfo.RowsBean>(jobProList, R.layout.rv_text) {
                    private void initView(View vi) {
                        tv = (TextView) vi.findViewById(R.id.tv);
                    }

                    private TextView tv;

                    @Override
                    protected void initR(View vi, JobProfessionInfo.RowsBean rowsBean, int p) {
                        initView(vi);
                        tv.setText(rowsBean.getProfessionName());
                        tv.setOnClickListener(view -> {
//                            typeId = rowsBean.getId();
                            th(() -> {
                                postsList = Http.getInstance().get("/prod-api/api/job/post/list?professionId=" + rowsBean.getId(), null, JobPostInfo.class, 1).getRows();
                                ui(() -> {
                                    initAdapter(postsList);
                                    rv2.setAdapter(rvAd2);
                                });
                            });
                        });
                    }
                };
                rv1.setLayoutManager(new GridLayoutManager(getContext(), 3));
                rv1.setAdapter(rvAd1);
                initAdapter(postList);
                rv2.setLayoutManager(new LinearLayoutManager(getContext()));
                rv2.setAdapter(rvAd2);
                search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                    @Override
                    public boolean onQueryTextSubmit(String query) {
                        List<JobPostInfo.RowsBean> list = new ArrayList<>();
                        for (JobPostInfo.RowsBean q : postList) {
                            if (!(q.getProfessionName() == null)) {
                                if (q.getProfessionName().contains(query)) {
                                    list.add(q);
                                }
                            }
                        }
                        List<JobPostInfo.RowsBean> postSList = new ArrayList<>(list);
                        initAdapter(postSList);
                        rv2.setAdapter(rvAd2);
                        return true;
                    }

                    @Override
                    public boolean onQueryTextChange(String newText) {
                        return false;
                    }
                });
            }
        });
    }

    private void initAdapter(List<JobPostInfo.RowsBean> postList) {
        rvAd2 = new RvAd<JobPostInfo.RowsBean>(postList, R.layout.rv_base) {
            private void initView(View vi) {
                ig = (ImageView) vi.findViewById(R.id.ig);
                view = (View) vi.findViewById(R.id.view);
                lineRvPatient = (LinearLayout) vi.findViewById(R.id.lineRvPatient);
                tv1 = (TextView) vi.findViewById(R.id.tv1);
                tv2 = (TextView) vi.findViewById(R.id.tv2);
                tv3 = (TextView) vi.findViewById(R.id.tv3);
                tv4 = (TextView) vi.findViewById(R.id.tv4);
                tv5 = (TextView) vi.findViewById(R.id.tv5);
                tv6 = (TextView) vi.findViewById(R.id.tv6);
                tv7 = (TextView) vi.findViewById(R.id.tv7);
                imgRvPatient = (ImageView) vi.findViewById(R.id.imgRvPatient);
                imgDowm = (ImageView) vi.findViewById(R.id.imgDowm);
                rv = (RecyclerView) vi.findViewById(R.id.rv);
                ig.setVisibility(View.GONE);
                view.setVisibility(View.GONE);
            }

            private RecyclerView rv;
            private ImageView imgDowm;
            private ImageView imgRvPatient;
            private TextView tv7;
            private TextView tv6;
            private TextView tv5;
            private TextView tv4;
            private TextView tv3;
            private TextView tv2;
            private TextView tv1;
            private LinearLayout lineRvPatient;
            private View view;
            private ImageView ig;

            @Override
            protected void initR(View vi, JobPostInfo.RowsBean rowsBean, int p) {
                initView(vi);
                tv1.setText("公司名称：" + rowsBean.getCompanyName());
                tv2.setText("职位名称：" + rowsBean.getProfessionName());
                tv3.setText("职位职责：" + rowsBean.getName());
                tv4.setText("公司地点：" + rowsBean.getAddress());
                tv5.setText("薪资待遇：" + rowsBean.getSalary() + "元/月");
                vi.setOnClickListener(view1 -> {
                    jobPost = rowsBean;
                    add(new JobPostFragment());
                });
            }
        };
    }

    private void initView(View iv) {
        banner = (Banner) iv.findViewById(R.id.banner);
        search = (SearchView) iv.findViewById(R.id.search);
        rv1 = (RecyclerView) iv.findViewById(R.id.rv1);
        rv2 = (RecyclerView) iv.findViewById(R.id.rv2);
    }
}
